home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / bipl.zip / PROCS.ZIP / EBCDIC.ICN < prev    next >
Text File  |  1992-11-20  |  6KB  |  156 lines

  1. ############################################################################
  2. #
  3. #    File:     ebcdic.icn
  4. #
  5. #    Subject:  Procedures to convert between ASCII and EBCDIC
  6. #
  7. #    Author:   Alan Beale
  8. #
  9. #    Date:     March 31, 1990
  10. #
  11. ###########################################################################
  12. #
  13. #     These procedures assist in use of the ASCII and EBCDIC character sets,
  14. #     regardless of the native character set of the host:
  15. #
  16. #     Ascii128()    Returns a 128-byte string of ASCII characters in
  17. #                   numerical order.  Ascii128() should be used in
  18. #                   preference to &ascii for applications which might
  19. #                   run on an EBCDIC host.
  20. #
  21. #     Ascii256()    Returns a 256-byte string representing the 256-
  22. #                   character ASCII character set.  On an EBCDIC host,
  23. #                   the order of the second 128 characters is essentially
  24. #                   arbitrary.
  25. #
  26. #     Ebcdic()      Returns a 256-byte string of EBCDIC characters in
  27. #                   numerical order.
  28. #
  29. #     AsciiChar(i)  Returns the character whose ASCII representation is i.
  30. #
  31. #     AsciiOrd(c)   Returns the position of the character c in the ASCII
  32. #                   collating sequence.
  33. #
  34. #     EbcdicChar(i) Returns the character whose EBCDIC representation is i.
  35. #
  36. #     EbcdicOrd(c)  Returns the position of the character c in the EBCDIC
  37. #                   collating sequence.
  38. #
  39. #     MapEtoA(s)    Maps a string of EBCDIC characters to the equivalent
  40. #                   ASCII string, according to a plausible mapping.
  41. #
  42. #     MapAtoE(s)    Maps a string of ASCII characters to the equivalent
  43. #                   EBCDIC string, according to a plausible mapping.
  44. #
  45. #     Control(c)    Returns the "control character" associated with the
  46. #                   character c.  On an EBCDIC host, with $ representing
  47. #                   an EBCDIC character with no 7-bit ASCII equivalent,
  48. #                   Control("$") may not be identical to "\^$", as
  49. #                   translated by ICONT (and neither result is particularly
  50. #                   meaningful).
  51. #
  52. #   Notes:
  53. #
  54. #       There is no universally accepted mapping between ASCII and EBCDIC.
  55. #       See the SHARE Inc. publication "ASCII and EBCDIC Character Set and
  56. #       Code Issues in Systems Application Architecture" for more information
  57. #       than you would ever want to have on this subject.
  58. #
  59. #       The mapping of the first 128 characters defined below by Ascii128()
  60. #       is the most commonly accepted mapping, even though it probably
  61. #       is not exactly like the mapping used by your favorite PC to mainframe
  62. #       file transfer utility.  The mapping of the second 128 characters
  63. #       is quite arbitrary, except that where an alternate translation of
  64. #       ASCII char(n) is popular, this translation is assigned to
  65. #       Ascii256()[n+129].
  66. #
  67. #       The behavior of all functions in this package is controlled solely
  68. #       by the string literals in the _Eascii() procedure.  Therefore you
  69. #       may modify these strings to taste, and still obtain consistent
  70. #       results, provided that each character appears exactly once in the
  71. #       result of _Eascii().
  72. #
  73. #       Yes, it's really true that the EBCDIC "\n" (NL, char(16r15)) is not
  74. #       the same as "\l" (LF, char(16r25)).  How can that be?  "Don't blame
  75. #       me, man, I didn't do it."
  76. #
  77. ############################################################################
  78.  
  79. procedure _Eascii()
  80.     static EinAorder
  81.     initial
  82.         EinAorder :=
  83. #            NUL SOH STX ETX EOT ENQ ACK BEL BS  HT  NL  VT  FF  CR  SO  SI
  84.            "\x00\x01\x02\x03\x37\x2d\x2e\x2f\x16\x05\x15\x0b\x0c\x0d\x0e\x0f"||
  85. #            DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM  SUB ESC FS  GS  RS  US
  86.            "\x10\x11\x12\x13\x3c\x3d\x32\x26\x18\x19\x3f\x27\x1c\x1d\x1e\x1f"||
  87. #            sp  !   "   #   $   %   &   '   (   )   *   +   ,   -   .   /
  88.            "\x40\x5a\x7f\x7b\x5b\x6c\x50\x7d\x4d\x5d\x5c\x4e\x6b\x60\x4b\x61"||
  89. #            0   1   2   3   4   5   6   7   8   9   :   ;   <   =   >   ?
  90.            "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\x7a\x5e\x4c\x7e\x6e\x6f"||
  91. #            @   A   B   C   D   E   F   G   H   I   J   K   L   M   N   O
  92.            "\x7c\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xd1\xd2\xd3\xd4\xd5\xd6"||
  93. #            P   Q   R   S   T   U   V   W   X   Y   Z   $<  \   $>  ^   _
  94.            "\xd7\xd8\xd9\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xad\xe0\xbd\x5f\x6d"||
  95. #            `   a   b   c   d   e   f   g   h   i   j   k   l   m   n   o
  96.            "\x79\x81\x82\x83\x84\x85\x86\x87\x88\x89\x91\x92\x93\x94\x95\x96"||
  97. #            p   q   r   s   t   u   v   w   x   y   z   $(  |   $)  ~   DEL
  98.            "\x97\x98\x99\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xc0\x4f\xd0\xa1\x07"||
  99.            "\x04\x06\x08\x09\x0a\x14\x17\x1a\x1b\x20\x25\x21\x22\x23\x24\x28_
  100.             \x29\x2a\x2b\x2c\x30\x31\x33\x34\x35\x36\x38\x39\x3a\x3b\x3e\xff_
  101.             \x41\x42\x43\x44\x4a\x45\x46\x47\x48\x49\x51\x52\x53\x54\x55\x56_
  102.             \x57\x58\x59\x62\x63\x64\x65\x66\x67\x68\x69\x70\x71\x72\x73\x74_
  103.             \x75\x76\x77\x78\x80\x8a\x8c\x8d\x8e\x8f\x90\x9a\x9c\x9d\x9e\x9f_
  104.             \xa0\xaa\xab\xac\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9_
  105.             \xba\xbb\xbc\xbe\xbf\xca\xcb\xcc\xcd\xce\xcf\xda\xdb\xdc\xdd\xde_
  106.             \xdf\xe1\xea\xeb\xec\xed\xee\xef\xfa\xfb\xfc\x8b\x6a\x9b\xfd\xfe"
  107.     return EinAorder
  108. end
  109.  
  110. procedure Ascii128()
  111.     if "\l" == "\n" then return string(&ascii)
  112.     return _Eascii()[1+:128]
  113. end
  114.  
  115. procedure Ascii256()
  116.     if "\l" == "\n" then return string(&cset)
  117.     return _Eascii()
  118. end
  119.  
  120. procedure Ebcdic()
  121.     if "\l" ~== "\n" then return &cset
  122.     return map(&cset, _Eascii(), &cset)
  123. end
  124.  
  125. procedure AsciiChar(i)
  126.     if "\l" == "\n" then return char(i)
  127.     return _Eascii()[0 < i+1] | runerr(205,i)
  128. end
  129.  
  130. procedure AsciiOrd(c)
  131.     if "\l" == "\n" then return ord(c)
  132.     return ord(MapEtoA(c))
  133. end
  134.  
  135. procedure EbcdicChar(i)
  136.     if "\l" ~== "\n" then return char(i)
  137.     return map(char(i), _Eascii(), &cset)
  138. end
  139.  
  140. procedure EbcdicOrd(c)
  141.     if "\l" ~== "\n" then return ord(c)
  142.     return ord(MapAtoE(c))
  143. end
  144.  
  145. procedure MapEtoA(s)
  146.     return map(s, _Eascii(), &cset)
  147. end
  148.  
  149. procedure MapAtoE(s)
  150.     return map(s, &cset, _Eascii())
  151. end
  152.  
  153. procedure Control(c)
  154.     return AsciiChar(iand(AsciiOrd(c),16r1f))
  155. end
  156.